package com.funmi.lizip.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.entity.ProcessIndex;
import com.funmi.lizip.entity.dto.ProcessIndex.ProcessIndexAddRequest;
import com.funmi.lizip.entity.dto.ProcessIndex.ProcessIndexQueryRequest;
import com.funmi.lizip.entity.dto.ProcessIndex.ProcessIndexUpdateRequest;
import com.funmi.lizip.entity.vo.ProcessIndexVo;
import com.funmi.lizip.service.ProcessIndexService;
import com.funmi.lizip.utils.MyJsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "过程指标管理模块")
@RestController
@RequestMapping("/processIndex")
public class ProcessIndexController {

    @Autowired
    private ProcessIndexService processIndexService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "分页获取 过程指标信息")
    @GetMapping("")
    public R getList(ProcessIndexQueryRequest request) throws IllegalAccessException {
        long current = 1;
        long size = 10;
        ProcessIndexQueryRequest processIndexQueryRequest = MyJsonUtil.setNullValue(request);
        ProcessIndex processIndexQuery = new ProcessIndex();
        if (request != null) {
            BeanUtils.copyProperties(processIndexQueryRequest, processIndexQuery);
            current = request.getPage();
            size = request.getPageSize();
        }
        QueryWrapper<ProcessIndex> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        /*if (!ObjectUtils.isEmpty(request) && !ObjectUtils.isEmpty(request.getTitle())){
            queryWrapper.like("title", request.getTitle());
        }*/
        if (!ObjectUtils.isEmpty(request) && !ObjectUtils.isEmpty(request.getType())){
            queryWrapper.like("type", request.getType());
        }
        if (!ObjectUtils.isEmpty(request) && !ObjectUtils.isEmpty(request.getContent())){
            queryWrapper.like("content", request.getContent());
        }
        if (!ObjectUtils.isEmpty(request) && !ObjectUtils.isEmpty(request.getTitle())){
            queryWrapper.like("title", request.getTitle());
        }

        Page<ProcessIndex> processIndexPage = processIndexService.page(new Page<>(current, size), queryWrapper);

        PageDTO<ProcessIndexVo> processIndexVoPage = new PageDTO<>(processIndexPage.getCurrent(), processIndexPage.getSize(), processIndexPage.getTotal());
        List<ProcessIndexVo> processIndexVoList = processIndexPage.getRecords().stream().map(processIndex -> {
            ProcessIndexVo processIndexVo = new ProcessIndexVo();
            BeanUtils.copyProperties(processIndex, processIndexVo);
            return processIndexVo;
        }).collect(Collectors.toList());
        processIndexVoPage.setRecords(processIndexVoList);
        return R.success().data(processIndexVoPage);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除 过程指标信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable int id) {
        boolean b = processIndexService.removeById(id);
        if (b) {
            return R.success().message("删除成功");
        }
        return R.success().message("删除失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "新增 过程指标信息")
    @PostMapping("")
    public R add(@RequestBody ProcessIndexAddRequest request) throws IOException {
        if (request == null) {
            return R.success().data("数据为空");
        }
        ProcessIndex processIndex = new ProcessIndex();
        BeanUtils.copyProperties(request, processIndex);
        boolean save = processIndexService.save(processIndex);
        if (save) {
            return R.success().message("新增成功");
        }
        return R.success().message("新增失败");
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据id 获取 过程指标信息")
    @GetMapping("/{id}")
    public R get(@PathVariable int id) {
        ProcessIndex processIndex = processIndexService.getById(id);
        if (ObjectUtils.isEmpty(processIndex)){
            return R.fail().message("无数据或被删除");
        }
        ProcessIndexVo processIndexVo = new ProcessIndexVo();
        BeanUtils.copyProperties(processIndex, processIndexVo);
        return R.success().data(processIndexVo);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改 过程指标信息内容")
    @PutMapping("")
    public R update(@RequestBody ProcessIndexUpdateRequest request) {
        ProcessIndex processIndex = new ProcessIndex();
        BeanUtils.copyProperties(request, processIndex);
        boolean b = processIndexService.updateById(processIndex);
        if (b) {
            return R.success().message("修改成功");
        }
        return R.fail().message("修改失败");
    }
/*
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "统计 干扰类型 数量")
    @GetMapping("/Statistics")
    public R Statistics() {
        ProcessIndexStatisticsVo vo = new ProcessIndexStatisticsVo();
        QueryWrapper<ProcessIndex> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT type");
        List<ProcessIndex> list = processIndexService.list(queryWrapper);
        HashMap<String, Long> map = new HashMap<>();
        list.forEach(processIndexs -> {
            map.put(processIndexs.getType(), processIndexService.count(new QueryWrapper<ProcessIndex>().eq("type",processIndexs.getType())));
        });
        vo.setType(map);
        // 从2022年1月份起 至今。统计每月的干扰数量
        LinkedHashMap<String, Long> month = new LinkedHashMap<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime of = LocalDateTime.of(2022, 1, 1, 0, 0);
        for (int j = 0; j < (now.getYear()+1 - of.getYear()); j++) {
            for (int i = of.getMonthValue(); i <= now.getMonthValue(); i++) {
                System.out.println(of.getYear()+"-"+i);
                month.put(of.getYear()+"-"+i,
                        processIndexService.count(new QueryWrapper<ProcessIndex>().between("gmt_found"
                                ,LocalDateTime.of(j+of.getYear(), i, 1, 0, 0)
                                , LocalDateTime.of(j+of.getYear(), i,
                                        LocalDateTime.of(j+of.getYear(),i,1,1,1).with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth()
                                        , 23, 59)))
                        );
            }
        }
        vo.setMonths(month);
        return R.success().data(vo);
    }*/

}
